package com.coderworm.webscoketdemo.util;

/**
 * @author: qiao.binbin@ustcinfo.com
 * @date: 2025/7/10 12:20
 * @version: 1.0
 * @description:
 */
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationUtil implements ApplicationContextAware {
    public static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public static <T> T getBeanName(String beanName, Class<T> cls) {
        return context.getBean(beanName, cls);
    }

    public static <T> T getBean(Class<T> cls) {
        return context.getBean(cls);
    }

    public static String getProperty(String key) {
        return context.getEnvironment().getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
        return context.getEnvironment().getProperty(key, defaultValue);
    }

    public static <T> T getProperty(String key, Class<T> cls) {
        return context.getEnvironment().getProperty(key, cls);
    }

    public static <T> T getProperty(String key, Class<T> cls, T defaultValue) {
        return context.getEnvironment().getProperty(key, cls, defaultValue);
    }
}
